package com.kgc.ljp.server.dao.imp;

import com.kgc.ljp.server.dao.AdminDao;
import com.kgc.ljp.server.entity.*;
import com.kgc.ljp.server.util.Init;
import com.kgc.ljp.server.util.SetUtil;

import java.util.HashMap;
import java.util.Map;

public class AdminDaoImp implements AdminDao {
    static Map<String,Admin> adminMap = new HashMap<>();
    static Map<String,User> userMap = new HashMap<>();
    static Map<String,HeroClass> heroClassMap = new HashMap<>();
    static Map<String,KunClass> kunClassMap = new HashMap<>();
    static Map<String,JuQingPoint> juQingPointMap = new HashMap<>();
    static {
        adminMap = (Map<String, Admin>) SetUtil.readObj("Admin.txt");
        if (adminMap==null){
            adminMap = Init.adminMap;
        }
        userMap = (Map<String, User>) SetUtil.readObj("User.txt");
        if (userMap == null) {
            userMap = Init.userMap;
        }
        heroClassMap = (Map<String, HeroClass>) SetUtil.readObj("HeroClass.txt");
        if (heroClassMap == null) {
            heroClassMap = Init.heroClassMap;
        }
        kunClassMap = (Map<String, KunClass>) SetUtil.readObj("KunClass.txt");
        if (kunClassMap == null) {
            kunClassMap = Init.kunClassMap;
        }
        juQingPointMap = (Map<String, JuQingPoint>) SetUtil.readObj("JuQingMap.txt");
        if (juQingPointMap == null) {
            juQingPointMap = Init.juQingPointMap;
        }
        SetUtil.writeObj(userMap, "User.txt");
        SetUtil.writeObj(heroClassMap, "HeroClass.txt");
        SetUtil.writeObj(kunClassMap, "KunClass.txt");
        SetUtil.writeObj(juQingPointMap, "JuQingMap.txt");
    }

    /**
     * 管理员注册
     * @param admin
     * @return
     */
    @Override
    public boolean regiest(Admin admin) {
        adminMap = (Map<String, Admin>) SetUtil.readObj("Admin.txt");
        if (adminMap==null){
            adminMap = Init.adminMap;
        }
        if (adminMap.containsKey(admin.getName())){
            return false;
        }else {
            adminMap.put(admin.getName(),admin);
            SetUtil.writeObj(adminMap,"Admin.txt");
            return true;
        }

    }

    /**
     * 添加卡片
     * @param heroClass
     * @return
     */
    @Override
    public boolean addCard(HeroClass heroClass) {
        heroClassMap = (Map<String, HeroClass>) SetUtil.readObj("HeroClass.txt");
        if (heroClassMap==null){
            heroClassMap = Init.heroClassMap;
        }
        if (heroClassMap.containsKey(heroClass.getName())){
            return false;
        }else{
            heroClassMap.put(heroClass.getName(),heroClass);
            SetUtil.writeObj(heroClassMap,"HeroClass.txt");
            return true;
        }
    }

    /**
     * 修改卡片
     * @param cardName
     * @return
     */
    @Override
    public boolean updateCard(String cardName) {
        heroClassMap = (Map<String, HeroClass>) SetUtil.readObj("HeroClass.txt");
        if (heroClassMap==null){
            heroClassMap = Init.heroClassMap;
        }

        SetUtil.writeObj(heroClassMap,"HeroClass.txt");
        return false;
    }

    /**
     * 添加地图
     * @param juQingPoint
     * @return
     */
    @Override
    public boolean addMap(JuQingPoint juQingPoint) {
        juQingPointMap = (Map<String, JuQingPoint>) SetUtil.readObj("JuQingMap.txt");
        if (juQingPointMap==null){
            juQingPointMap = Init.juQingPointMap;
        }
        if(juQingPointMap.containsKey(juQingPoint.getcName())){
            return false;
        }else{
            juQingPointMap.put(juQingPoint.getcName(),juQingPoint);
            SetUtil.writeObj(juQingPointMap,"JuQingMap.txt");
            return true;
        }

    }

    /**
     * 封禁玩家
     * @param userName
     * @return
     */
    @Override
    public boolean deleteUser(String userName) {
        userMap = (Map<String, User>) SetUtil.readObj("User.txt");
        if(userMap==null){
            userMap = Init.userMap; }
        if (userMap.containsKey(userName)){
            userMap.remove(userName);
            SetUtil.writeObj(userMap,"User.txt");
            return true;
        }else{
            return false;
        }
    }
    public Map selectAll(){
        return userMap;
    }
}
